<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
    <title>Mqtt Websockets</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js" type="text/javascript"></script>
    <script src="/vue.min.js" type="text/javascript"></script>

    <style type="text/css">
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 8px 20px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
        }
    </style>

    <script type="text/javascript">
        var vm = new Vue({
            el: '#push',
            data: {
                client: new Paho.MQTT.Client(location.hostname, 15675, "/ws", "clientid_" + parseInt(Math.random() * 100, 10)),
                topic: "push_message_topic"
            },
            mounted () {
                this.init();
            },
            methods: {
                init: function () {
                    var options = {
                        timeout: 3,
                        keepAliveInterval: 30,
                        onSuccess: function () {
                            console.log(("连接成功~"));
                            this.client.subscribe(this.topic, {qos: 1});
                        },
                        onFailure: function (message) {
                            console.log(("连接失败~" + message.errorMessage));
                        }
                    };
                    if (location.protocol == "https:") {
                        options.useSSL = true;
                    }
                    //console.log(("CONNECT TO " + wsbroker + ":" + wsport));
                    this.client.connect(options);
                }
            },

            onConnectionLost: function () {

            },

            onMessageArrived:function () {
                this.client.onMessageArrived = function (message) {
                    console.log("RECEIVE ON " + message.destinationName + " PAYLOAD " + message.payloadString);
                    console.log((message.payloadString));
                };
            }
        })
    </script>

</head>

<div id="push">
    <div style="margin-top: 30px;">
        <input style="height: 25px; width: 180px;" maxlength="60" value="这是一条测试消息" id="testMessage"/>
        <button class="button" id="mySendBtn" onclick="testMessageSend()"> 点击发送</button>
        <button class="button" id="cleanSendBtn" onclick="testMessageClean()"> 清空日志</button>
    </div>
    <div style="margin-top: 30px">
        <div style="font-size: 20px;color: darkcyan"> 接收到的mqtt消息日志</div>
        <hr/>
        <div id="arrivedDiv" style="height:600px; width:1000px; overflow:scroll; background:#EEEEEE;">
            <br/>
        </div>
    </div>
</div>
</html>
